#include "loginwidget.h"
#include "ui_loginwidget.h"
#include<QLabel>
#include<QMovie>
#include<QComboBox>
#include<QMouseEvent>
#include"reg.h"
#include<QIcon>
#include<QPushButton>
#include "srchpassword.h"
#include<QSqlDatabase>
#include<QDebug>
#include<QSqlQuery>
#include"chatlist.h"
#include<QVector>
#include<QStringList>
#include<QCompleter>
#include"chat.h"
#include<QTcpServer>
#include<QTcpSocket>
#include<QHostAddress>
#include "ui_setnetwork.h"
#include "ui_chat.h"
#include "ui_chatlist.h"
#include "ui_reg.h"
#include "ui_spasswordnext2.h"
#include "pass.h"
#include "ui_pass.h"




loginwidget::loginwidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::loginwidget)
{
    ui->setupUi(this);
    setMouseTracking(true);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);


    QMovie *head_movie =new QMovie(this);
    head_movie->setFileName("://image/78fb9715d0b788381d5f32f4cb196583(1).gif");
    head_movie->start();
    ui->headgif->setMovie(head_movie);
    ui->account->lineEdit()->setPlaceholderText("DewnDewn号/手机/邮箱");//先要获取下落框的小部件之一的行编辑区
    ui->headimage->setMask(QRegion(ui->headimage->rect(),QRegion::RegionType::Ellipse));//设置掩码

    reg.setWindowTitle("新用户注册");
    reg.setWindowIcon(QIcon("://image/b90d814b2e3ec614556318aabff4a42.jpg"));

    search.setWindowTitle("找回密码");
    search.setWindowIcon(QIcon("://image/b90d814b2e3ec614556318aabff4a42.jpg"));


    ui->regbtn->setDown(true);
     ui->loginbtn->setDown(true);


    connect(ui->regbtn,&QPushButton::pressed,[=](){
        this->close();
        reg.show();
    });

    connect(&reg,&reg::cancle,[=](){
       this->show();
    });

    connect(ui->passwordbtn,&QPushButton::pressed,[=](){
        this->close();
        search.show();
    });

    connect(&search,&srchpassword::cancle,[=](){
       this->show();
    });

    connect(&reg,&reg::regok,this,&loginwidget::regdeal);

    connect(&reg,&reg::closeEvent,[=](){
        this->show();
    });

    connect(&chatlist,&chatlist::createchatbox,this,&loginwidget::dealcreate);



}
int loginwidget::usersnum=0;
loginwidget::~loginwidget()
{
    delete ui;
}


void loginwidget::on_close_clicked()
{
    this->hide();
}

void loginwidget::on_min_clicked()
{
    this->showMinimized();
}
void loginwidget::mousePressEvent(QMouseEvent *e){
    if(e->button()==Qt::LeftButton){
        p=e->globalPos()-this->frameGeometry().topLeft();
    }
    else if(e->button()==Qt::RightButton){
        return;
    }
}
void loginwidget::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons()==Qt::LeftButton){//获取的长时间的按键是左键
        this->move(e->globalPos()-p);
    }
}

void loginwidget::on_loginbtn_clicked()
{
    password=ui->ecoedit->text();
    for(int i=0;i<usersnum;i++){
        if((useraccount[i]==accountnum&&userpass[i]==password)||(phonenum[i]==accountnum&&userpass[i]==password)){
            this->chatlist.show();
            this->hide();
            return;

        }
        else if((useraccount[i]==accountnum&&userpass[i]!=password)||(phonenum[i]==accountnum&&userpass[i]!=password)){
            ui->passremind->setText("密码错误");
            return;
        }
    }
    ui->accountremind->setText("该用户不存在");


}


void loginwidget::on_account_currentTextChanged(const QString &)
{
    accountnum=ui->account->currentText();
}
void loginwidget::regdeal(QString str1,QString str2,QString str3,QString str4){
    namedewn=str1;
    dewnname.push_back(str1);
    useraccount.push_back(str2);
    userpass.push_back(str3);
    phonenum.push_back(str4);
    chatlist.ui->name->setText(namedewn);
    usersnum++;
}

void loginwidget::dealcreate(QString str){
    int x;
    for(int i=0;i<chatlist.membernum;i++){
        if(str==chatlist.chatname[i]){
            x=i;
        }
    }
    chatlist.chatboxs[x]->show();
        connect(chatlist.chatboxs[x]->ui->sendbtn,&QPushButton::clicked,this,[=](){
            QString string=chatlist.chatboxs[x]->ui->textEdit->toPlainText();
            chatlist.chatboxs[x]->ui->textBrowser->append(QString("(我)"+string));
            chatlist.chatboxs[x]->ui->textEdit->clear();
            for(int i=0;i<chatlist.membernum&&i!=x;i++){
                chatlist.chatboxs[i]->ui->textBrowser->append((QString("(好友)"+string)));


            }

        });

    //客户端发消息





}


void loginwidget::on_net_clicked()
{
    netbox.show();
}



